<?php	
		function dangonline()
		{
				$sql="SELECT 	sum( if( username <>'' , 1 , 0 ) ) As SoThanhVien, 
				sum( if( username ='' , 1 , 0 ) ) As SoKhach , 
				count(*) As TongSoNguoi 
			FROM sessions";
			$kq=mysql_query($sql);
			$row=mysql_fetch_assoc($kq);
			return $row['TongSoNguoi'] + 15;
		}
		
		if(!isset($_SESSION['counter1']))
		{
			$sql="select * from counter2 where id=1";
			$kq=mysql_query($sql);
			$row_kq=mysql_fetch_assoc($kq);
			$r_homnay=$row_kq['homnay'];
			$r_homnay=explode(':',$r_homnay);
			$r_homnay_ngay=$r_homnay[0];
			$r_homnay_count=$r_homnay[1];
			
			$r_homqua=$row_kq['homqua'];
			$r_homqua=explode(':',$r_homqua);
			$r_homqua_ngay=$r_homqua[0];
			$r_homqua_count=$r_homqua[1];
			//echo $r_homnay_ngay;die;
			if((strtotime(date('Y-m-d'))-(strtotime(date($r_homnay_ngay))+86400))/86400!=0)
			{
				$date=0;
			}
			if((strtotime(date('Y-m-d'))-(strtotime(date($r_homnay_ngay))+86400))/86400==0)
			{
				$date=1;
			}
			if((strtotime(date('Y-m-d'))==(strtotime(date($r_homnay_ngay)))))
			{
				$date=2;
				
			}
			switch($date){
			
				 case 0:
					$r_homnay_ngay=date('Y-m-d');
					$r_homnay_count=1;
					
					$r_homqua_ngay=date("Y-m-d", strtotime("-1 day"));
					$r_homqua_count=0;
					break;
				 case 1:
				 	$r_homnay_ngay=date('Y-m-d');
					$r_homnay_count=1;
					
					$r_homqua_ngay=date("Y-m-d", strtotime("-1 day"));
					$r_homqua_count=$r_homnay[1];
					break;
				 case 2:
				 	$r_homnay_ngay=date('Y-m-d');
					
					$r_homnay_count=$r_homnay_count+1;
					break;
			}
			
			$r_thangnay=$row_kq['thangnay'];
			$r_thangnay=explode(':',$r_thangnay);
			$r_thangnay_ngay=$r_thangnay[0];
			$r_thangnay_count=$r_thangnay[1];
			
			$r_thangtruoc=$row_kq['thangtruoc'];
			$r_thangtruoc=explode(':',$r_thangtruoc);
			$r_thangtruoc_ngay=$r_thangtruoc[0];
			$r_thangtruoc_count=$r_thangtruoc[1];
			
			
			$ngay = date("d",strtotime(date($r_thangnay_ngay)));
			$thang = date("m",strtotime(date($r_thangnay_ngay)));
			$nam = date("Y",strtotime(date($r_thangnay_ngay)));
			$thang1 = date("m",mktime(0,0,0,$thang+1,$ngay,$nam));
			
			if((date('m')-$thang1)!=0)
			{
				$month=0;
			}
			if((date('m')-$thang1)==0)
			{
				$month=1;
			}
			if(date('m')==$thang)
			{
				$month=2;
			}
			
			switch($month){
			
				 case 0:
					$r_thangnay_ngay=date('Y-m-d');
					$r_thangnay_count=1;
					
					$r_thangtruoc_ngay=date("Y-m-d", strtotime("-1 month"));
					$r_thangtruoc_count=0;
					break;
				 case 1:
				 	$r_thangnay_ngay=date('Y-m-d');
					$r_thangnay_count=1;
					
					$r_thangtruoc_ngay=date("Y-m-d", strtotime("-1 month"));
					$r_thangtruoc_count=$r_thangnay[1];
					break;
				 case 2:
				 	$r_thangnay_ngay=date('Y-m-d');
					$r_thangnay_count=$r_thangnay_count+1;
					break;
			}
			//nam
			$r_namnay=$row_kq['namnay'];
			$r_namnay=explode(':',$r_namnay);
			$r_namnay_ngay=$r_namnay[0];
			$r_namnay_count=$r_namnay[1];
			
			$r_namtruoc=$row_kq['namtruoc'];
			$r_namtruoc=explode(':',$r_namtruoc);
			$r_namtruoc_ngay=$r_namtruoc[0];
			$r_namtruoc_count=$r_namtruoc[1];
			
			
			$ngay_nam = date("d",strtotime(date($r_namnay_ngay)));
			$thang_nam = date("m",strtotime(date($r_namnay_ngay)));
			$nam_nam = date("Y",strtotime(date($r_namnay_ngay)));
			
			$nam1 = date("Y",mktime(0,0,0,$thang_nam,$ngay_nam,$nam_nam+1));
			if((date('Y')-$nam1)!=0)
			{
				$year=0;
			}
			if((date('Y')-$nam1)==0)
			{
				$year=1;
			}
			if(date('Y')==$nam_nam)
			{
				$year=2;
			}
			switch($year){
			
				 case 0:
					$r_namnay_ngay=date('Y-m-d');
					$r_namnay_count=1;
					
					$r_namtruoc_ngay=date("Y-m-d", strtotime("-1 year"));
					$r_namtruoc_count=0;
					break;
				 case 1:
				 	$r_namnay_ngay=date('Y-m-d');
					$r_namnay_count=1;
					
					$r_namtruoc_ngay=date("Y-m-d", strtotime("-1 year"));
					$r_namtruoc_count=$r_namnay[1];
					break;
				 case 2:
				 	$r_namnay_ngay=date('Y-m-d');
					$r_namnay_count=$r_namnay_count+1;
					break;
			}
			//tuan
			//echo (date("l"));die;
			if(date("l")!='Monday') $thisweek = (date("d", strtotime("Monday")-86400*7));
			else $thisweek = (date("d", strtotime("Monday")));
			
			$r_tuannay=$row_kq['tuannay'];
			$r_tuannay=explode(':',$r_tuannay);
			$r_tuannay_ngay=$r_tuannay[0];
			$r_tuannay_count=$r_tuannay[1];
			
			$r_tuantruoc=$row_kq['tuantruoc'];
			$r_tuantruoc=explode(':',$r_tuantruoc);
			$r_tuantruoc_ngay=$r_tuantruoc[0];
			$r_tuantruoc_count=$r_tuantruoc[1];
			
			$tuan_tuan=date("l",strtotime(date($r_tuannay_ngay)));
			if($tuan_tuan=='Monday') $tuan1=date("d",strtotime(date($r_tuannay_ngay)));
			elseif($tuan_tuan=='Tuesday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*1);
			elseif($tuan_tuan=='Wednesday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*2);
			elseif($tuan_tuan=='Thursday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*3);
			elseif($tuan_tuan=='Friday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*4);
			elseif($tuan_tuan=='Saturday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*5);
			elseif($tuan_tuan=='Sunday') $tuan1=date("d",strtotime(date($r_tuannay_ngay))-86400*6);
			//echo $tuan1;die;
			if(($thisweek-($tuan1+7))!=0)
			{
				$week=0;
			}
			if(($thisweek-($tuan1+7))==0)
			{
				$week=1;
			}
			if($thisweek==$tuan1)
			{
				$week=2;
			}
			//echo $week;die;
			
			switch($week){
			
				 case 0:
					$r_tuannay_ngay=date('Y-m-d');
					$r_tuannay_count=1;
					
					$r_tuantruoc_ngay=date("Y-m-d", strtotime("-7 day"));
					$r_tuantruoc_count=0;
					break;
				 case 1:
				 	$r_tuannay_ngay=date('Y-m-d');
					$r_tuannay_count=1;
					
					$r_tuantruoc_ngay=date("Y-m-d", strtotime("-7 day"));
					$r_tuantruoc_count=$r_tuannay[1];
					break;
				 case 2:
				 	$r_tuannay_ngay=date('Y-m-d');
					$r_tuannay_count=$r_tuannay_count+1;
					break;
			}
			
			$homnay=$r_homnay_ngay.':'.$r_homnay_count;
			$homqua=$r_homqua_ngay.':'.$r_homqua_count;
			$thangnay=$r_thangnay_ngay.':'.$r_thangnay_count;
			$thangtruoc=$r_thangtruoc_ngay.':'.$r_thangtruoc_count;
			$namnay=$r_namnay_ngay.':'.$r_namnay_count;
			$namtruoc=$r_namtruoc_ngay.':'.$r_namtruoc_count;
			$tuannay=$r_tuannay_ngay.':'.$r_tuannay_count;
			$tuantruoc=$r_tuantruoc_ngay.':'.$r_tuantruoc_count;
			$tongluot=$row_kq['tongluot'];
			
			$sql="update counter2 set homnay='$homnay', homqua='$homqua', thangnay='$thangnay', thangtruoc='$thangtruoc'
			, namnay='$namnay' , namtruoc='$namtruoc', tuannay='$tuannay', tuantruoc='$tuantruoc', tongluot=$tongluot+1";
			$kq=mysql_query($sql);
			
			$_SESSION['counter1']['allcounter']=$tongluot+100000;
			$_SESSION['counter1']['today']=$r_homnay_count;
			$_SESSION['counter1']['yesterday']=$r_homqua_count;
			$_SESSION['counter1']['thisweek']=$r_tuannay_count;
			$_SESSION['counter1']['lastweek']=$r_tuantruoc_count;
			$_SESSION['counter1']['thismonth']=$r_thangnay_count;
			$_SESSION['counter1']['lastmonth']=$r_thangtruoc_count;
			$_SESSION['counter1']['thisyear']=$r_namnay_count;
			$_SESSION['counter1']['lastyear']=$r_namtruoc_count;
			
			
			
				
////////////////////////////////////////// HIEN DU LIEU RA BAN COUTER //////////////////////				
///				/*
////				<tr>
///						<td><img src='images/v-today.jpg'> Đang online</td>
///						<td><div>".online()."</div></td>
///				  </tr>
//				<tr>
//					<td><img src='images/v-yesterday.jpg'> Hôm qua</td>
//					<td><div>".$_SESSION['counter1']['yesterday']."</div></td>
//				  </tr>
////				*/
///					 <tr>
//					<td><img src='images/v-week.jpg'> Tuần trước</td>
//					<td><div>".$_SESSION['counter1']['lastweek']."</div></td>
//				  </tr>
//
//
// <tr>
//					<td><img src='images/v-month.jpg'> Tháng trước</td>
//					<td><div>".$_SESSION['counter1']['lastmonth']."</div></td>
//				  </tr>
//
///////////////////////////////////////////////////////////////////////////////////////

	$t=$_SESSION['counter1']['allcounter'];
	
	$j=6;
	if($t>=1000000&&$t<10000000) $j=7;
	if($t>=10000000&&$t<100000000) $j=8;
	$i=$j;
	while($i>0)
	{
		$d=$t%10;
		$array[$i]=$d;
		$i--;
		$t=intval($t/10);
	}
	$hitcount='';
			$i=1;
			while($i<=$j)
			{
			if($array[$i]==0)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/0.png'>";
			}
		 	if($array[$i]==1)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/1.png'>";
			}

			if($array[$i]==2)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/2.png'>";
			}

if($array[$i]==3)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/3.png'>";
			}

if($array[$i]==4)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/4.png'>";
			}

if($array[$i]==5)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/5.png'>";
			}


if($array[$i]==6)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/6.png'>";
			}

if($array[$i]==7)
			{
				$hitcount.="<img src='".$duongdan."images/counter/7.png'>";
			}

if($array[$i]==8)
			{
				$hitcount.="<img src='".$duongdan."images/counter/8.png'>";
			}

if($array[$i]==9)
			{
				$hitcount.="<img src='".$duongdan."images/counter/9.png'>";
			}
$i++;}
				echo"
				<table border='0' cellspacing='0' cellpadding='1' width='160' align='center' style='padding-left:4px;' >
				 <td align='center' colspan='2'>".$hitcount."
				 </td>
				  <tr>
					<td style='text-align:left;color:#333333'>Online: </td>
					<td><div align='left' style='color:#830000;font-weight:bold;text-align:right' >".dangonline()."</div></td>
				  </tr>
				   <tr>
					<td style='text-align:left;color:#333333'>Total: </td>
					<td><div align='left' style='color:#830000;font-weight:bold;text-align:right' >".$_SESSION['counter1']['allcounter']."</div></td>
				  </tr>
				</table>
				";
		}
		else{
		$t=$_SESSION['counter1']['allcounter'];
	
	
	$j=6;
	if($t>=1000000&&$t<10000000) $j=7;
	if($t>=10000000&&$t<100000000) $j=8;
	$i=$j;
	while($i>0)
	{
		$d=$t%10;
		$array[$i]=$d;
		$i--;
		$t=intval($t/10);
	}
	$hitcount='';
			$i=1;
			while($i<=$j)
			{
			if($array[$i]==0)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/0.png'>";
			}
		 	if($array[$i]==1)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/1.png'>";
			}

			if($array[$i]==2)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/2.png'>";
			}

if($array[$i]==3)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/3.png'>";
			}

if($array[$i]==4)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/4.png'>";
			}

if($array[$i]==5)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/5.png'>";
			}


if($array[$i]==6)
			{
				$hitcount.= "<img src='".$duongdan."images/counter/6.png'>";
			}

if($array[$i]==7)
			{
				$hitcount.="<img src='".$duongdan."images/counter/7.png'>";
			}

if($array[$i]==8)
			{
				$hitcount.="<img src='".$duongdan."images/counter/8.png'>";
			}

if($array[$i]==9)
			{
				$hitcount.="<img src='".$duongdan."images/counter/9.png'>";
			}
$i++;}
			echo"
				<table border='0' cellspacing='0' cellpadding='1' width='160' align='center' style='padding-left:4px;' >
				 <td align='center' colspan='2'>".$hitcount."
				 </td>
				  <tr>
					<td style='text-align:left;color:#333333'>Online: </td>
					<td><div align='left' style='color:#830000;font-weight:bold;text-align:right' >".dangonline()."</div></td>
				  </tr>
				   <tr>
					<td style='text-align:left;color:#333333'>Total: </td>
					<td><div align='left' style='color:#830000;font-weight:bold;text-align:right' >".$_SESSION['counter1']['allcounter']."</div></td>
				  </tr>
				</table>
				";
		}

?>